﻿using System;
using System.Linq;
using System.Xml.Serialization;
using System.IO;
namespace e1
{  
    [Serializable]
    public class Order
    {
        public int orderNum;
        public string product;
        public string customer;
        public double price;

        public override bool Equals(Object obj)
        {
            if (obj is Order)
            {
                Order m = (Order)obj;
                if (m.orderNum == this.orderNum && m.customer == this.customer && m.product == this.product && m.price == this.price)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return "订单号:" + this.orderNum + " 产品:" + this.product + " 顾客姓名:" + this.customer + " 订单金额:" + this.price;
        }
        public Order() { }
        public Order(int on, string p, string c, double pri)
        {
            this.orderNum = on;
            this.product = p;
            this.customer = c;
            this.price = pri;
        }
        public void setOrderNum(int on)
        {
            this.orderNum = on;
        }
        public void setProduct(string p)
        {
            this.product = p;
        }
        public void setCustomer(string c)
        {
            this.customer = c;
        }
        public void setPrice(double pri)
        {
            this.price = pri;
        }
        public int getOrderNum()
        {
            return this.orderNum;
        }
        public string getProduct()
        {
            return this.product;
        }
        public string getCustomer()
        {
            return this.customer;
        }
        public double getPrice()
        {
            return this.price;
        }
    }
    [Serializable]
    class OrderService
    {
        public static Order[] orderList;
        public static int length;
        public static void XmlSerialize(XmlSerializer ser,string filename,object obj)
        {
            FileStream fs=new FileStream(filename,FileMode.Create);
            ser.Serialize(fs, obj);
            fs.Close();

        }
        public static object XmlDeserialize(XmlSerializer ser,string filename)
        {
            try { 
                FileStream fs=new FileStream(filename,FileMode.Open);
                object obj = ser.Deserialize(fs);
                fs.Close();
                return obj;
            }
            catch
            {
                return null;
            }
           

        }
        public static void exportList()//将所有的订单序列化为XML文件
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(Order[]));
            String xmlFileName = "order.xml";
            XmlSerialize(xmlser, xmlFileName, orderList);
            string xml = File.ReadAllText(xmlFileName);
            Console.WriteLine(xml);
        }
        public static void importList()
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(Order[]));
            orderList = XmlDeserialize(xmlser, "order.xml")as Order[];
            for(int i = 0; orderList[i] != null; i++)
            {
                length++;
            }
        }
            public static void ordering()
            {//排序算法
                Order tmp;
                for (int j = length - 1; j > 0; j--)
                {
                    for (int i = 0; i < j; i++)
                    {
                        if (orderList[i].getOrderNum() > orderList[i + 1].getOrderNum())
                        {
                            /*tmp = orderList[i].getOrderNum();
                            orderList[i].setOrderNum(orderList[i + 1].getOrderNum());
                            orderList[i + 1].setOrderNum(tmp);*/
                            tmp = orderList[i];
                            orderList[i] = orderList[i + 1];
                            orderList[i + 1] = tmp;
                        }
                    }
                }
            }
            public OrderService()
            {
                orderList = new Order[100];
                length = 0;
            }
            public static void addOrder()
            {
                Console.WriteLine("请输入订单号:");
                string num_s = Console.ReadLine();
                int num = Convert.ToInt32(num_s);
                Console.WriteLine("请输入产品名:");
                string productName = Console.ReadLine();
                Console.WriteLine("请输入顾客名:");
                string customerName = Console.ReadLine();
                Console.WriteLine("请输入订单金额:");
                string pri_s = Console.ReadLine();
                double pri = Convert.ToDouble(pri_s);
                //Order o = new Order(num, productName, customerName, pri);
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(length);
                bool okToAdd = true;
                foreach (var item in allOrders)
                {
                    /*if (item.Equals(o))
                    {
                        o = null;
                        okToAdd = false;
                    }*/
                    if (item != null)
                    {
                        if (item.getOrderNum() == num | (item.getProduct() == productName && item.getCustomer() == customerName && item.getPrice() == pri))
                        {//保证每个订单的明细不重复
                            okToAdd = false;
                        }
                    }


                }
                if (okToAdd)
                {
                    Order o = new Order(num, productName, customerName, pri);
                    orderList[length] = o;
                    length++;
                    Console.WriteLine("添加成功");
                }
                else
                {
                    Console.WriteLine("添加失败，已有相同订单");
                }

            }
            public static void deleteOrder()
            {
                Console.WriteLine("请输入需要删除订单的订单号:");
                string num_s = Console.ReadLine();
                int num = Convert.ToInt32(num_s);
                bool find = false;
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(length);
                int n = 0;
                foreach (var item in allOrders)
                {
                    n++;
                    if (item.getOrderNum() == num)
                    {
                        find = true;
                        //int j = n;
                        while (n < length)
                        {
                            orderList[n - 1] = orderList[n];
                            n++;
                        }
                        length--;
                        Console.WriteLine("删除成功");
                    }
                }
                if (!find)
                {
                    Console.WriteLine("无此订单");
                }
            }
            public static void updateOrder()
            {
                Console.WriteLine("请输入需要更改订单的订单号:");
                string num_s = Console.ReadLine();
                int num = Convert.ToInt32(num_s);
                bool find = false;
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(length);
                int n = 0;
                foreach (var item in allOrders)
                {
                    if (item.getOrderNum() == num)
                    {
                        bool okToUpdate = true;
                        Console.WriteLine("请输入更改后的产品:");
                        string new_product = Console.ReadLine();
                        Console.WriteLine("请输入更改后的顾客:");
                        string new_customer = Console.ReadLine();
                        Console.WriteLine("请输入更改后的金额:");
                        string new_price_s = Console.ReadLine();
                        double new_price = Convert.ToDouble(new_price_s);
                        var allOrders2 = from item2 in orderList where item2 != null select item2;
                        allOrders2 = allOrders2.Take(length);

                        foreach (var item2 in allOrders2)
                        {
                            if (item.getProduct() == new_product && item.getCustomer() == new_customer && item.getPrice() == new_price)
                            {
                                okToUpdate = false;
                            }
                        }
                        if (okToUpdate)
                        {
                            orderList[n].setProduct(new_product);
                            orderList[n].setCustomer(new_customer);
                            orderList[n].setPrice(new_price);
                            find = true;
                            Console.WriteLine("修改成功！");
                        }
                        else
                        {
                            Console.WriteLine("修改失败，已有相同订单");
                        }


                    }
                    n++;
                }
                if (!find)
                {
                    Console.WriteLine("无此订单");
                }
            }

            public static void selectOrder()
            {
                Console.WriteLine("请选择查询方式：");
                Console.WriteLine("1.订单号");
                Console.WriteLine("2.产品");
                Console.WriteLine("3.顾客");
                Console.WriteLine("4.金额");
                string choice = Console.ReadLine();
                if (choice == "1")
                {
                    Console.WriteLine("请输入订单号:");
                    string num_s = Console.ReadLine();
                    int num = Convert.ToInt32(num_s);
                    bool find = false;
                    var allOrders = from item in orderList where item != null && item.getOrderNum() == num select item;
                    allOrders = allOrders.Take(length);
                    foreach (var item in allOrders)
                    {
                        Console.WriteLine(item.ToString());
                        find = true;
                    }
                    if (!find)
                    {
                        Console.WriteLine("无订单");
                    }
                }
                else if (choice == "2")
                {
                    Console.WriteLine("请输入产品名:");
                    string productName = Console.ReadLine();
                    bool find = false;
                    var allOrders = from item in orderList where item != null && item.getProduct() == productName select item;
                    allOrders = allOrders.Take(length);
                    foreach (var item in allOrders)
                    {
                        Console.WriteLine(item.ToString());
                        find = true;
                    }
                    if (!find)
                    {
                        Console.WriteLine("无订单");
                    }
                }
                else if (choice == "3")
                {
                    Console.WriteLine("请输入顾客名：");
                    string customerName = Console.ReadLine();
                    bool find = false;
                    var allOrders = from item in orderList where item != null && item.getCustomer() == customerName select item;
                    allOrders = allOrders.Take(length);
                    foreach (var item in allOrders)
                    {
                        Console.WriteLine(item.ToString());
                        find = true;
                    }
                    if (!find)
                    {
                        Console.WriteLine("无订单");
                    }
                }
                else if (choice == "4")
                {
                    Console.WriteLine("请输入订单金额:");
                    string pri_s = Console.ReadLine();
                    double pri = Convert.ToDouble(pri_s);
                    bool find = false;
                    var allOrders = from item in orderList where item != null && item.getPrice() == pri select item;
                    allOrders = allOrders.Take(length);
                    foreach (var item in allOrders)
                    {
                        Console.WriteLine(item.ToString());
                        find = true;
                    }
                    if (!find)
                    {
                        Console.WriteLine("无订单");
                    }
                }
                else
                {
                    Console.WriteLine("输入错误");
                }

            }

        }
    class Program
    {
        
          
        static void Main(string[] args)
        {
            OrderService os = new OrderService();
            OrderService.importList();
            while (true)
            {

                Console.WriteLine("欢迎进入订单管理系统！");
                Console.WriteLine("请选择：");
                Console.WriteLine("1.查询订单");
                Console.WriteLine("2.添加订单");
                Console.WriteLine("3.修改订单");
                Console.WriteLine("4.删除订单");
                Console.WriteLine("5.退出");
                string choice = Console.ReadLine();
                if (choice == "1")
                {
                    /*Console.WriteLine(OrderService.length);
                    for(int i = 0; i < OrderService.length; i++)
                    {
                        Console.WriteLine(OrderService.orderList[i].ToString());
                    }*/
                    OrderService.ordering();
                    OrderService.selectOrder();


                }
                else if (choice == "2")
                {
                    OrderService.addOrder();
                    /* Console.WriteLine(OrderService.length);
                     for (int i = 0; i < OrderService.length; i++)
                     {
                         Console.WriteLine(OrderService.orderList[i].ToString());
                     }*/
                }
                else if (choice == "3")
                {
                    OrderService.updateOrder();
                }
                else if (choice == "4")
                {
                    OrderService.deleteOrder();
                }
                else if (choice == "5")
                {
                    System.Environment.Exit(0);
                }
                else
                {
                    Console.WriteLine("输入错误");
                }
                OrderService.exportList();

            }
            
        }
    
    }
}
